package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.LogVO;
import com.icss.beans.PlanVO;
import com.icss.daos.LogDao;
import com.icss.daos.PlanDao;

public class PlanServlet extends HttpServlet {

	/**
	 * 工作计划
	 */
	public PlanServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String plan=request.getParameter("plan");
		
		PlanDao dao=new PlanDao();
		if(plan!=null){
			if(plan.equals("add")){
				Date beginTime=null;
				Date endTime=null;
				String planName=request.getParameter("txtName");
				if(request.getParameter("txtBeginDate")!=null&&request.getParameter("txtBeginDate")!=""){
					beginTime=Date.valueOf(request.getParameter("txtBeginDate"));
	               } 
				if(request.getParameter("txtEndDate")!=null&&request.getParameter("txtEndDate")!=""){
					endTime=Date.valueOf(request.getParameter("txtEndDate"));
	               }	
				int state=Integer.parseInt(request.getParameter("RadioGroup"));
				String planType=request.getParameter("txtType");
				String planPartner=request.getParameter("ParticipatorName");
				String managerName=request.getParameter("ManagerName");
				String planRemark=request.getParameter("txtRemark");
				String planPercent=request.getParameter("sltCompletedPercent");
				String planContent=request.getParameter("txtContent");
				String sql="insert into workPlan(planName,beginTime,endTime,planType,planPartner,managerName,planRemark,planPercent,planContent,planState) values('"+planName+"','"+beginTime+"','"+endTime+"','"+planType+"','"+planPartner+"','"+managerName+"','"+planRemark+"','"+planPercent+"','"+planContent+"','"+state+"')";
				System.out.println("SQL语句为"+sql);
				dao.insertPlan(sql);
			 }
			
			if(plan.equals("query")){	
				List<PlanVO> list=new ArrayList<PlanVO>();
				Date beginDate=null;
				Date endDate=null;
				if(request.getParameter("txtBeginDate")!=null&&request.getParameter("txtBeginDate")!=""){
					beginDate=Date.valueOf(request.getParameter("txtBeginDate"));
	               } 
				if(request.getParameter("txtEndDate")!=null&&request.getParameter("txtEndDate")!=""){
					endDate=Date.valueOf(request.getParameter("txtEndDate"));
	               }
				String planType=request.getParameter("txtType");
				String planName=request.getParameter("txtName");
				String planRemark=request.getParameter("txtRemark");			
				String sql=null;
				
				if(planType!=null&&planType!=""){
					sql="select * from workPlan where planType='"+planType+"'";
				}
				
					if(beginDate!=null){
						sql = sql + " and beginTime >=   '"+beginDate+"'";
					}
					if(endDate!=null){
						sql = sql + " and endTime <=   '"+endDate+"'";
					}
				
				if(planName!=null&&planName!=""){
					sql = sql + " and planName like  '"+planName+"'";
				}
				if(planRemark!=null&&planRemark!=""){
					sql=sql+"and planRemark like '"+planRemark+"'";
				}
				System.out.println("SQL语句为："+sql);
				
				list=dao.selectPlan(sql);
				if(list!=null){
					
					
					request.getSession().setAttribute("planVo", list);
	       			   String url="/page/office/plan/edit_plan.jsp";
	       			   RequestDispatcher rd=request.getRequestDispatcher(url);
	       			   rd.forward(request, response);   
	        	  }else{System.out.println("没有匹配的数据");}	
		 }
			}else{System.out.println("隐藏域没值");}
			
		}


	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
